<?php
namespace common\models\person;
use Yii;
use services\Application;
use common\helpers\ArrayHelper;
use common\models\person\Cla;

/**
 * This is the model class for table "{{%sys_notify}}".
 *
 * @property string $id 主键
 * @property string $title 标题
 * @property string $content 消息内容
 * @property int $type 消息类型[1:公告;2:提醒;3:信息(私信)
 * @property int $target_id 目标id
 * @property string $target_type 目标类型
 * @property int $target_display 接受者是否删除
 * @property string $action 动作
 * @property int $view 浏览量
 * @property int $sender_id 发送者id
 * @property int $sender_display 发送者是否删除
 * @property int $sender_withdraw 是否撤回 0是撤回
 * @property int $status 状态[-1:删除;0:禁用;1启用]
 * @property int $created_at 创建时间
 * @property string $updated_at 修改时间
 */
class Person extends \common\models\base\BaseModel
{
    public $hobby_id = [];
    public $code = '';
    public $province_id = '';
    public $city_id = '';
    public $area_id = '';
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%person}}';
    }

    /**
     * {@inheritdoc}common下的model
     * 修改时走的验证规则 没有验证的为不安全数据  不让增加
     * 验证数据为 为安全数据 则显示搜搜框
     */
    public function rules()
    {
        $session = \Yii::$app->session;
        return [
            [['name', 'tel','sex','cla_id','mail'], 'required'],
            [['name','img'], 'string'],
            [['hobby_id'], 'safe'],
            ['mail', 'email'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '姓名',
            'sex' => '性别',
            'img' => '图片',
            'cla_id' => '班级',
            'class' => '班级',
            'hobby_id' => '爱好',
            'tel' => '手机号',
            'status' => '状态',
            'mail' => '邮箱',
            'code' => '验证码',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    /**
     * 关联班级表
     *
     * @return \yii\db\ActiveQuery
     */
    public function getCla()
    {
        return $this->hasOne(Cla::class, ['id' => 'cla_id']);
    }
    /**
     * @return \yii\db\ActiveQuery
     * @throws \yii\base\InvalidConfigException
     */
    public function getHobbys()
    {
        return $this->hasMany(Hobby::class, ['id' => 'hobby_id'])
            ->viaTable(PersonHobbyMap::tableName(), ['hobby_id' => 'id'])
            ->asArray();
    }



    /**
     * @param bool $insert
     * @return bool

    public function beforeSave($insert)
    {
        if (!$this->isNewRecord && $this->status <= StatusEnum::DISABLED) {
            NotifyManager::updateAll(['status' => $this->status], ['notify_id' => $this->id]);
        }
        return parent::beforeSave($insert);
    }*/
}
